<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>校园导航</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <style>
        header {
            background-color: #f2f2f2;
            line-height: 42px;
            font-size: 18px;
            text-align: center;
            height: 42px;
            position: relative;
        }

        header b {
            width: 42px;
            height: 100%;
            display: inline-block;
            position: relative;
            z-index: 1;
            background: url() no-repeat center;
            background-size: 70% auto;
        }

        header b.ic-back {
            background-image: url(../image/ic-back.png);
        }

        header b.ic-check {
            background-image: url(../image/ic-check.png);
            float: right;
        }

        header .title {
            position: absolute;
            left: 0;
            bottom: 0;
            right: 0;
            text-align: center;
            font-size: 18px;
        }

        .flex-con {
            overflow: auto
        }
    </style>
</head>

<body>
    <div id="wrap" class="flex-wrap flex-vertical">
        <header class="border-b">
            <div class="title">校园导航</div>
            <b tapmode="hover" onclick="funActionSheet();" class="ic-check"></b>
        </header>
        <div id="main" class="flex-con">
        </div>
    </div>
</body>

</html>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
    apiready = function() {
        $api.fixStatusBar($api.dom('header'));
        var school = $api.getStorage('school');
        var editor = $api.getStorage('editor');
        if (school) {
            if (editor) {
                $api.dom('.title').innerHTML = school.name + ": " + editor.realName;
            } else {
                $api.dom('.title').innerHTML = school.name;
            }
            api.openFrame({
                name: 'home_main',
                url: 'home_main.html',
                rect: {
                    x: 0,
                    y: $api.dom('header').offsetHeight,
                    w: api.winWidth,
                    h: $api.dom('#main').offsetHeight
                }
            });
        } else {
            api.openWin({
                name: 'entrance',
                url: '../entrance.html'
            });
        }
        api.setStatusBarStyle({
            style: 'dark',
            color: '#6ab494'
        });

        api.addEventListener({
            name: 'updateStorage'
        }, function(ret, err) {
            school = $api.getStorage('school');
            editor = $api.getStorage('editor');
            if (editor) {
                $api.dom('.title').innerHTML = school.name + ": " + editor.realName;
            } else {
                $api.dom('.title').innerHTML = school.name;
            }

        });
    };

    function funActionSheet() {
        api.actionSheet({
            title: '更多选项',
            cancelTitle: '取消',
            buttons: ['切换学校', '学校编辑登录', '注销']
        }, function(ret, err) {
            editor = $api.getStorage('editor')
            if (ret.buttonIndex == 1) {
                $api.rmStorage('school');
                api.openWin({
                    name: 'entrance',
                    url: '../entrance.html'
                });
                api.closeWin({
                    name: 'home'
                });
            } else if (ret.buttonIndex == 2) {
                if (editor) {
                    api.toast({
                        msg: '您已经登录',
                        duration: 2000,
                        location: 'bottom'
                    });
                } else {
                    api.openWin({
                        name: 'editor_login',
                        url: './editor_login.html'
                    });
                    api.closeWin({
                        name: 'home'
                    });
                }
            } else if (ret.buttonIndex == 3) {
                if (editor) {
                    $api.rmStorage('editor');
                    api.toast({
                        msg: '注销成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({
                        name: 'updateStorage'
                    });
                    api.confirm({
                        title: '是否前往登录',
                        buttons: [ '确定', '取消']
                    }, function(ret, err) {
                        var index = ret.buttonIndex;
                        if (index === 1) {
                            api.openWin({
                                name: 'editor_login',
                                url: './editor_login.html'
                            });
                            api.closeWin({
                                name: 'home'
                            });
                        }
                    });
                } else {
                    api.toast({
                        msg: '您还没有登录',
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }
        });
    }
</script>
